package com.sinya.shoppingmall;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinya.shoppingmall.bean.Constants;
import com.sinya.shoppingmall.fragment.CartFragment;
import com.sinya.shoppingmall.fragment.HomeFragment;
import com.sinya.shoppingmall.fragment.MineFragment;
import com.sinya.shoppingmall.fragment.RecommendFragment;
import com.sinya.shoppingmall.fragment.SearchFragment;
import com.sinya.shoppingmall.https.VolleyParams;
import com.sinya.shoppingmall.https.VolleyRequest;
import com.sinya.shoppingmall.ui.recomend.MenuItemView;
import com.sinya.shoppingmall.ui.recomend.MenuItemView.onMenuItemClickListener;
import com.sinya.shoppingmall.utils.FileHelper;
import com.sinya.shoppingmall.utils.ImageLoaderUtils;
import com.sinya.shoppingmall.utils.LogUtils;

import cn.sharesdk.framework.ShareSDK;

/**
 * 主页
 *
 * @author KoizumiSinya
 * @date 2015/12/23. 23:14
 */

public class TableActivity extends FragmentActivity implements onMenuItemClickListener {

    private Context mContext;
    private SlidingPaneLayout mSlidingPaneLayout;
    private ImageView mUserIcon;
    private TextView mUserName;


    public SlidingPaneLayout getSlidingPaneLayout() {
        return this.mSlidingPaneLayout;
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:

                    break;
                default:
                    break;
            }

            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;

        initSharedSDK();
        initSlidelayout();
        initMenuList();
        initHomeFragments();

        getData();
    }

    /**
     * 初始化SharedSDK
     */
    private void initSharedSDK() {
        ShareSDK.initSDK(this);
    }

    private void initSlidelayout() {
        mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.SlidingPane);
        mSlidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {

            }

            @Override
            public void onPanelOpened(View panel) {

            }

            @Override
            public void onPanelClosed(View panel) {

            }
        });
    }

    private void initMenuList() {

        mUserIcon = (ImageView) findViewById(R.id.img_user_icon);
        updateUserIcon();

        mUserName = (TextView) findViewById(R.id.tv_user_name);

        LinearLayout menuLayout = (LinearLayout) findViewById(R.id.menu);
        MenuItemView view = new MenuItemView(mContext);
        view.setView(1, R.mipmap.menu_chat, "消息", this);
        menuLayout.addView(view);

        view = new MenuItemView(mContext);
        view.setView(2, R.mipmap.menu_contacts, "我的好友", this);
        menuLayout.addView(view);

        view = new MenuItemView(mContext);
        view.setView(3, R.mipmap.menu_ticket, "代金券", this);
        menuLayout.addView(view);

        view = new MenuItemView(mContext);
        view.setView(4, R.mipmap.menu_mypurse, "我的钱包", this);
        menuLayout.addView(view);

        view = new MenuItemView(mContext);
        view.setView(5, R.mipmap.menu_red_packet, "我的红包", this);
        menuLayout.addView(view);

        view = new MenuItemView(mContext);
        view.setView(6, R.mipmap.menu_cart, "购物车", this);
        menuLayout.addView(view);

        view = new MenuItemView(mContext);
        view.setView(7, R.mipmap.menu_setting, "设置", this);
        menuLayout.addView(view);
    }

    private void updateUserIcon() {
        //从Sd中找头像，转换成Bitmap
        Bitmap bt = BitmapFactory.decodeFile(FileHelper.getSDPath() + Constants.USER_PICTURE_NAME);
        if (bt != null && mUserIcon != null) {
            //转换成drawable
            mUserIcon.setImageBitmap(ImageLoaderUtils.getCirlceImage(bt));
        } else {
            //TODO 如果SD里面没有则需要从服务器取头像，取回来的头像再保存在SD中
        }
    }

    private void initHomeFragments() {
        FragmentTabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(mContext, getSupportFragmentManager(), android.R.id.tabcontent);

        //取消每个导航View之间的竖线
        tabHost.getTabWidget().setDividerDrawable(null);

        LinearLayout layout;
        layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.tab_one, null, false);
        tabHost.addTab(tabHost.newTabSpec("one").setIndicator(layout), HomeFragment.class, null);
        layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.tab_two, null, false);
        tabHost.addTab(tabHost.newTabSpec("two").setIndicator(layout), RecommendFragment.class, null);
        layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.tab_three, null, false);
        tabHost.addTab(tabHost.newTabSpec("three").setIndicator(layout), SearchFragment.class, null);
        layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.tab_four, null, false);
        tabHost.addTab(tabHost.newTabSpec("four").setIndicator(layout), CartFragment.class, null);
        layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.tab_five, null, false);
        tabHost.addTab(tabHost.newTabSpec("five").setIndicator(layout), MineFragment.class, null);

        //默认第一页
        tabHost.setCurrentTab(0);
    }

    private void getData() {
        VolleyRequest.Start(mContext, handler, 0, VolleyParams.login, "test", "test");
    }

    // [+] Override Method

    @Override
    protected void onResume() {
        updateUserIcon();
        super.onResume();
    }


    // [-] Override Method
    // [+] click

    /**
     * 进入用户资料
     *
     * @param veiw
     */
    public void intoUserData(View veiw) {
        LogUtils.I("Sinya", "设置用户资料");
    }

    /**
     * Menu列表点击
     *
     * @param id
     */
    @Override
    public void onMenuClick(int id) {
        switch (id) {
            case 1:
                LogUtils.I("Sinya", "进入消息界面");
                break;
            case 2:
                LogUtils.I("Sinya", "进入我的好友");
                break;
            case 3:
                LogUtils.I("Sinya", "进入代金券");
                break;
            case 4:
                LogUtils.I("Sinya", "进入我的钱包");
                break;
            case 5:
                LogUtils.I("Sinya", "进入我的红包");
                break;
            case 6:
                LogUtils.I("Sinya", "进入购物车");
                break;
            case 7:
                LogUtils.I("Sinya", "进入设置");
                break;
            default:
                break;
        }
    }

    // [-] click
}